package com.bwlcums.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import org.apache.log4j.Logger;

public class FileUtils {
	private static Logger logger = Logger.getLogger(FileUtils.class);
	
	public static String getClassPath() {
		return FileUtils.class.getResource("/").getPath();
	}

	public static String getWebRootPath() {
		int i = getClassPath().indexOf("WEB-INF");
		if(i>0){
			return getClassPath().substring(0, i);
		}
		return getClassPath();
	}

	public static String getDirFilePath() {
		Date now = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		String dirFile = "" + c.get(Calendar.YEAR) + File.separatorChar
				+ (c.get(Calendar.MONTH) + 1) + File.separatorChar
				+ c.get(Calendar.DATE) + File.separatorChar;
		return dirFile;
	}

	/**
	 * Properties文件操作
	 * 
	 * @param filepath
	 * @param key
	 * @param value
	 * @throws IOException
	 */
	public static void setPropertiesFileValue(String filepath, String key,
			String value) throws IOException {
		File propFile = new File(filepath);
		if (!propFile.exists()) {
			propFile.createNewFile();
		}
		InputStream inStream = new FileInputStream(propFile);
		Properties props = new Properties();
		props.load(inStream);
		props.setProperty(key, value);
		OutputStream fos = new FileOutputStream(propFile);
		props.store(fos, "Update :set [" + key + "= "+value+"]");
		logger.debug("Update :set [" + key + "= "+value+"]");;
	}

}
